package com.zw.gulimall.product.controller;

import com.zw.gulimall.product.entity.BrandEntity;
import com.zw.gulimall.product.service.BrandService;
import com.zw.gulimall.utils.PageUtils;
import com.zw.gulimall.utils.R;
import com.zw.gulimall.validator.group.AddGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;



/**
 * 品牌
 *
 * @author zhaowang
 * @email 137234570@qq.com
 * @date 2023-02-09 02:01:56
 */
@RestController
    @RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("product:brand:list")
    public R list(@RequestBody Map<String, Object> params){
//        int i = 4/0;
        PageUtils page = brandService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
//    @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:brand:save")
    public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand
//            , BindingResult result
    ){
//        if (result.hasErrors()){
//            HashMap<String, String> hashMap = new HashMap<>();
//            //校验错误结果
//            result.getFieldErrors().forEach(item->{
//                //获取错误的属性或名字
//                String field = item.getField();
//                //获取错误的信息
//                String message = item.getDefaultMessage();
//                hashMap.put(field,message);
//            });
//            return R.error(400,"提交信息不合法").put("data",hashMap);
//        } else {
            brandService.save(brand);
//        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:brand:update")
    public R update(@RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    @RequestMapping("/update/status")
//    @RequiresPermissions("product:brand:update")
    public R updateStatus(@RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
